Skip to content

feat(InputBinding): subscribe callback now supports event priority #4211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 19, 2025

Conversation

cpsievert
Copy link
Collaborator

@cpsievert cpsievert commented Apr 25, 2025

Until now, custom input bindings could only pass a boolean value to their subscribe callback. That is, do something similar to this to notify Shiny of a change:

subscribe = function(el, callback) {
  $(el).on("keyup.exampleComponentName", function(event) {
    callback(true); // or false
  });
}

These boolean values map to values of priority: "deferred" (true) and priority: "immediate" (false), meaning 'send later & debounce' or 'send now'.

With this PR, you can now:

  1. Specify the priority as an object, making the code more readable/explicit (i.e., callback({priority: "deferred"})
  2. Provide a new value of {priority: "event"} to always send now, even if the value hasn't changed.

For example:

subscribe = function(el, callback) {
  $(el).on("keyup.exampleComponentName", function(event) {
    callback({priority: "event"})
  });
}

The motivator for adding this is for input_submit_textarea() in rstudio/bslib#1204, where it feels like the most desirable behavior is to always resend immediately.

@cpsievert cpsievert added this to the Next Release milestone Apr 26, 2025
@cpsievert cpsievert force-pushed the feat/subscribeCallbackEvent branch from 95f1c08 to b77ef5e Compare May 1, 2025 17:39
@cpsievert cpsievert force-pushed the feat/subscribeCallbackEvent branch from b77ef5e to 157d489 Compare May 1, 2025 17:40
@cpsievert cpsievert requested a review from Copilot May 1, 2025 17:41
Copilot

This comment was marked as outdated.

@gadenbuie
Copy link
Member

gadenbuie commented May 2, 2025

These boolean values effectively map to values of priority: "deferred" (true) and priority: "immediate" (false).

This explanation is the first time I've seen callback(false) or callback(true) written out in a way that is clear, concise and makes sense to me. JavaScript's positional variables are inherently opaque, but the naming here doesn't help.

If we're updating the API, I'd love to push it in a direction that supports a more readable interface. I.e. in addition to backcompat for callback(<boolean>), it'd be nice if the augmented API could support

callback(true)
callback({ priority: "deferred" })

callback(false)
callback({ priority: "immediate" })

// or now also
callback({priority: "event"})

@cpsievert cpsievert requested a review from Copilot June 16, 2025 20:14
Copilot

This comment was marked as off-topic.

@cpsievert cpsievert requested a review from gadenbuie June 16, 2025 20:18
Copy link
Member

@gadenbuie gadenbuie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Just a small note to make sure the PR includes a demo snippet with the new feature

@cpsievert cpsievert merged commit b25e6fe into main Jun 19, 2025
11 checks passed
@cpsievert cpsievert deleted the feat/subscribeCallbackEvent branch June 19, 2025 15:27
schloerke added a commit that referenced this pull request Jul 6, 2025
* main: (32 commits)
  Revert the addition of spacing between `icon` and `label` in `actionButton()` (#4248)
  Restrict `icon`/`label` separator spacing to `actionButton()` (#4247)
  Fix front-end action button label updating logic (#4242)
  Update news
  Properly handle undefined value for input subscribe callback (#4243)
  Start new version (#4241)
  v1.11.0 release candidate (#4232)
  Follow up to #3996: fix front-end checkbox label updating logic (#4238)
  feat(InputBinding): subscribe callback now supports event priority (#4211)
  Follow up to #3996 when label is unspecified (i.e., NULL), don't include it in the message (#4237)
  Run routine (#4234)
  chore: #4175 update jquery-ui to 1.14.1 (#4205)
  Update jQuery to 3.7.1 (#3969)
  Fix 404 in example 08_html (shiny.min.css) (#4221)
  Follow up to #3870: fix location of news item (#4233)
  Bugfix for error found in tests (#3870)
  Allow update input labels with HTML (#3996)
  Adds mirai to documentation (#4230)
  family->given for R Core authorship (#4222)
  fix(renderPlot): get interactive plotting working with ggplot2 v4.0 (#4228)
  ...
vedhav added a commit to insightsengineering/teal.widgets that referenced this pull request Jul 30, 2025
Closes #308 

The subscribe callback now expects an object with the priority after
this PR rstudio/shiny#4211. So, if you update to
`shiny v1.11.1` the `draggable_buckets` do not work (the init works, but
since the subscribe is broken it does not update).

In the future we can think about using
[`sortable`](https://rstudio.github.io/sortable/) package to replace a
huge chunk of custom css/js code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants